//
//  AgreementAndPolicyView.m
//  jzyParents
//
//  Created by 任辰东 on 2022/4/25.
//

#import "AgreementAndPolicyView.h"
#import "UIButton+TouchAreaInsets.h"

@interface AgreementAndPolicyView ()
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) UILabel *markLabel;
@property (nonatomic, strong) UILabel *andLabel;
@property (nonatomic, strong) UIButton *policyBtn;
@property (nonatomic, strong) UIButton *privateBtn;
@end

@implementation AgreementAndPolicyView

-(instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

- (void)selectedBtnAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    if (self.didSelected) {
        self.didSelected(btn.selected);
    }
}

-(void)privateBtnAction
{

    if (self.didUserAgreement) {
        self.didUserAgreement();
    }
}

-(void)policyBtnAction
{
    if (self.didPrivacyPolicy) {
        self.didPrivacyPolicy();
    }
}

-(void)setupViews
{
    [self addSubview:self.selectedBtn];
    [self addSubview:self.markLabel];
    [self addSubview:self.andLabel];
    [self addSubview:self.policyBtn];
    [self addSubview:self.privateBtn];
    
    [self.selectedBtn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.privateBtn addTarget:self action:@selector(privateBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.policyBtn addTarget:self action:@selector(policyBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setupLayout
{
    [self.selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.left.equalTo(self);
    }];
    [self.selectedBtn setTouchAreaInsets:UIEdgeInsetsMake(25, 25, 25, 25)];
    
    [self.markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.selectedBtn.mas_right).offset(5);
        make.top.bottom.equalTo(self);
    }];
    
    [self.privateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.markLabel.mas_right);
        make.top.bottom.equalTo(self);
    }];
    
    [self.andLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.privateBtn.mas_right);
        make.top.bottom.equalTo(self);
    }];
    
    [self.policyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.andLabel.mas_right);
        make.top.right.bottom.equalTo(self);
    }];
}

#pragma mark -- lazyLoad

-(UIButton *)selectedBtn
{
    if (!_selectedBtn) {
        _selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectedBtn setImage:UIImageWithName(@"login_agree_n") forState:UIControlStateNormal];
        [_selectedBtn setImage:UIImageWithName(@"login_agree_h") forState:UIControlStateSelected];
    }
    return _selectedBtn;
}

-(UILabel *)markLabel
{
    if (!_markLabel) {
        _markLabel = [UILabel new];
        _markLabel.text = @"已阅读并同意以下条款：";
        _markLabel.textColor = UIColorFromHex(0x666666);
        _markLabel.font = UIFontSize(12);
    }
    return _markLabel;
}

-(UILabel *)andLabel
{
    if (!_andLabel) {
        _andLabel = [UILabel new];
        _andLabel.text = @"和";
        _andLabel.textColor = UIColorFromHex(0x666666);
        _andLabel.font = UIFontSize(12);
    }
    return _andLabel;
}

-(UIButton *)policyBtn
{
    if (!_policyBtn) {
        _policyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_policyBtn setTitle:@" 《隐私政策》  " forState:UIControlStateNormal];
        [_policyBtn setTitleColor:UIColorFromHex(kColorMain) forState:UIControlStateNormal];
        _policyBtn.titleLabel.font = UIFontSize(12);
    }
    return _policyBtn;
}

-(UIButton *)privateBtn
{
    if (!_privateBtn) {
        _privateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_privateBtn setTitle:@"  《用户协议》 " forState:UIControlStateNormal];
        [_privateBtn setTitleColor:UIColorFromHex(kColorMain) forState:UIControlStateNormal];
        _privateBtn.titleLabel.font = UIFontSize(12);
    }
    return _privateBtn;
}
@end
